<?php

/**
 * Description of ContentEmployeeReturning
 *
 * @author Honza, Petr
 */
class ContentEmployeeReturning implements IMainBodyWithInteraction {

  private $showTable = false;
  private $userID; //id of choosen user

  public function action() {
    $borrowingID = addslashes($_GET['ida']);
    //set date of returning to now
    if ($_GET['a'] == 'return') {
      $sql = 'update ' . TBL_BORROWINGS . ' set date_of_returning = CURDATE()';
      $sql .= ' where id_borrowing = ' . $borrowingID;
      DB::sql($sql);

      //is there a request for this book?
      $sql = 'select * from ' . TBL_BORROWINGS . '';
      $sql .= ' join ' . TBL_BOOK_PRINTS . ' using (id_book_print)';
      $sql .= ' join ' . TBL_BOOKS . ' using (id_book)';
      $sql .= ' where id_borrowing = ' . $borrowingID;
      $borrowing = DB::sql($sql);

      //is in waiting queue?
      $sql = '';
      $sql .= 'select * from ' . TBL_REQUESTS;
      $sql .= ' where id_book = ' . $borrowing[0]['id_book'] . ' and state = \'platný\'';
      $sql .= ' limit 1';
      $requirement = DB::sql($sql);



      if ($requirement) {
        //it is required, create new borrowing
        $borrowingID = $borrowing[0]['id_book_print'];
        $userID = $requirement[0]['id_user'];

        //create borrowing
        $sql = 'insert into ' . TBL_BORROWINGS . " values (0, $borrowingID, $userID, null,null,null,0)";
        DB::sql($sql);

        //dequeue user request for the book
        $sql = 'update ' . TBL_REQUESTS . ' set state= \'neplatný\' where id_request = ' . $requirement[0]['id_request'];
        DB::sql($sql);
      }
    }
  }

  public function applyForm($formID = NULL) {
    if ($formID == 1) {
      $this->userID = addslashes($_POST['user']);
      $this->showTable = true;
    }
  }

  public function getContent() {
    $content = '<h2>Book returning</h2>';
    $content .= $this->action();
    $content .= $this->getForm();
    $content .= $this->applyForm($this->getSubmitedForm());
    $content .= $this->getTable();

    return $content;
  }

  public function getForm($formID = NULL, $error = NULL) {
  	//draw form for choosing users
    if (!isset($_POST['userSubmit'])) {
      $f = new Form();
      $f->startForm('self', 'post', NULL, array("class" => "form_settings"));

      $sql = "select id_user, concat(name , ' ', surname) as name from " . TBL_USERS . ' ' .
              "join " . TBL_ROLES . ' using(id_role) ' .
              "where id_role = " . ROLE_USER_ID;

      $f->addLabel('User', 'user')->addSelectionBox('user', DB::sql($sql), 'name', 'id_user');
      $f->addLabel('&nbsp', 'userSubmit')->addInput('submit', 'userSubmit', 'Choose user', array("class" => "submit"));
      $f->endForm();

      return $f->getForm();
    }
    return '';
  }

  public function getSubmitedForm() {
    if (isset($_POST['userSubmit'])) {
      return 1;
    }

    return -1;
  }

  public function getTable($tableID = NULL) {
    if ($this->showTable) {
      //table of users borrowings
      $sql = 'select * from ' . TBL_BORROWINGS . '';
      $sql .= ' join ' . TBL_BOOK_PRINTS . ' using (id_book_print)';
      $sql .= ' join ' . TBL_BOOKS . ' using (id_book)';
      $sql .= ' where id_user = ' . $this->userID . ' and borrowed_until is not null and date_of_returning is null';
      $sql .= ' order by title';


      $result = DB::sql($sql);
      if (empty($result)) {
        return '<p>Nothing to return</p>';
      }


      $table = '<table>';
      $table .= '<tr>';
      $table .= '<th>Title</th><th>Barcode</th><th>Return</th>';
      $table .= '</tr>';
      foreach ($result as &$row) {
        $idVyp = $row['id_borrowing'];

        $table .= '<tr>';
        $table .= '<td>' . $row['title'] . '</td>';
        $table .= '<td>' . $idVyp . '</td>';

        $table .= '<td><a href="index.php?where=employee&subwhere=returning&a=return&ida=' . $idVyp . '">Return</a></td>';
        $table .= '</tr>';
      }
      $table .= '';
      $table .= '</table>';

      return $table;
    }
  }

}

?>
